import pandas as pd

import matplotlib
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.animation as animation
#from tftb.processing.cohen import WignerVilleDistribution, PageRepresentation, MargenauHillDistribution
from scipy.signal import hilbert, spectrogram, get_window

from numpy import log as ln
from datetime import date, timedelta
import tqdm

import Magnetic_Field, Velocity_diagrams

def create_table(table):
    c = 0
    max_c = 0
    index = 0
    index2 = 0
    for i in table:
        index += 1

        if i > 999:
            c = 0
        else:
            c += 1

        if c > max_c:
            index2 = index
            max_c = c

    return index2 - max_c, index2

def simple_plot(df1, df2, df3):

    fig = plt.figure(figsize=(12, 12), dpi=100)
    ax1 = fig.add_subplot(311)
    ax1.plot(df1)
    ax2 = fig.add_subplot(312)
    ax2.plot(df2)
    ax3 = fig.add_subplot(313)
    ax3.plot(df3)
    plt.show()

if __name__ == "__main__":

    #start, end = create_table(df_B["Bx_GSE"])
    #start, end = 309000, 314760
    # start = [386747 - 360, 255367 - 360, 196017 - 360, 91466, 51373 - 360]
    # start.reverse()
    # end = [386747 + 360, 255367 + 360, 196017 + 360, 92186, 51373 + 360]
    # end.reverse()
    # for i in range(len(start)):
    #     Magnetic_Field.B_Diagrams(df_B, start[i], end[i], i)
    #start, end = 2881, 2881 + 1440
    #start, end = 0, 7199
    #start, end = 7200, 7200*2 - 1
    #start, end = 6833, 6833 + 5000
    # start, end = 11521, 18720
    #start, end = 17633, 17633 + 1390
    #start, end = 1441, 31661 # 2023 c 50 по 70 день
    # start, end = 1443, 4321
    #start, end = 1443, 5759
    #start, end = 2881, 4321
    # start, end = 3800, 5000
    #start, end = 1, 1505
    # start, end = 10000, 315693
    # start, end = 1, 46503
    # start, end = 445000-2500, 445000+2500
    #start, end = 159843, 165567
    start, end = 26000-2500, 26000+2500
    start, end = start-1, end-1
    df_B = Magnetic_Field.Read_B_Table('B_2023_1min.lst')
    simple_plot(df_B["Bx_GSE"], df_B["By_GSE"], df_B["Bz_GSE"])

    Magnetic_Field.B_Diagrams(df_B, start, end, 1)

    #df_V = Velocity_diagrams.Read_V_Table('V_2023_1min.lst')
    #simple_plot(df_V["Vx_GSE"], df_V["Vy_GSE"], df_V["Vz_GSE"])
    # len = 0
    # max = 0
    # where = 0
    # for i,j in enumerate(df_V["Vx_GSE"][:]):
    #     if j > 9999:
    #         print("!")
    #         len = 0
    #     else:
    #         len += 1
    #     if len>max:
    #         where = i
    #         max=len
    # print(where, max)
    start, end = 111373, 115108
    # print(start, end)
    #Velocity_diagrams.V_Diagrams(df_V, start, end, 1)
